'use strict'
const { flaw_type_D } = require('../flaw_type');
const { detectParam_2 } = require('../detectParam');
const { piantiehou_db_config } = require('../config.json').station_db; // 源库
const { linedb_piantiehou_config } = require('../config.json').line_db; // 源库
const seq = require('../lib/db').getSequelizeInstance(piantiehou_db_config);
const seq1 = require('../lib/db').getSequelizeInstance(linedb_piantiehou_config);
const { Job_1, initJobData_1 } = require('../model/6/job');
const { initDoffData_1, Doff_1 } = require('../model/6/doff')
const { initPanelInfoData_1, Panel_info_1 } = require('../model/6/panel_info')
const { initFlawData_1, Flaw_1 } = require('../model/6/flaw')
const { initStation, station } = require('../model/6/station');
const DATE = require('../date')
class F {

    constructor() {
        initStation(seq1);
        initJobData_1(seq);
        initDoffData_1(seq);
        initPanelInfoData_1(seq);
        initFlawData_1(seq);
        this.flawUid = 0;
        this.flawArry = [];
        this.doffSum = 0;
        this.jobUid = 0;
        this.cuurntDoff = 0;
        this.time = DATE;
        this.init();
    }
    async init() {
        await this.getAsyncMaxPanelUid();
        this.createStatonData();
        this.createJob();
    }
    async getAsyncMaxPanelUid() {
        try {
            let job_uid = await Job_1.getMaxUid(seq);
            this.jobUid = job_uid;
            let flaw_uid = await Flaw_1.getMaxUid(seq);
            this.flawUid = flaw_uid;
        } catch (_) {
            console.log(_);
        }
    }

    createStatonData() {
        const stationData = flaw_type_D.map((item, index) => {
            return {
                id: index + 1,
                flaw_class_type: item.id,
                station_id: item.camera_id,
                station_name: item.station
            }
        })
        this.uploadInfo(station, stationData, 'station——目标')
    }
    async createPanelInfo(panelInfo, time) {
        this.time = time;
        if (panelInfo.flawCount) {
            panelInfo.flawCount = Math.floor(Math.random() * 20);
        }
        await this.createPanel(panelInfo, this.job);
        await this.createFlaw(panelInfo, this.job);
    }
    // flaw
    async createFlaw(panelInfo, job) {
        let flawArr = this.createFlawInfo(panelInfo, job);
        if (!flawArr) return false;
        this.uploadInfo(Flaw_1, flawArr, '偏贴后-Flaw');
    }
    // 创建各个缺陷
    createFlawInfo(panelInfo, job) {
        if (!panelInfo.flawCount) return;
        let flawArry = [];
        for (let i = 0; i < panelInfo.flawCount; i++) {
            const dWidth = Math.random() * 2;
            const dLength = Math.random() * 2;
            let flawTypeIndex = Math.floor(Math.random() * flaw_type_D.length);
            if (flawTypeIndex <= 7) {
                flawTypeIndex += 8;
            }
            flawArry.push({
                uid: ++this.flawUid,
                jobId: this.job.uid,
                doffId: panelInfo.uid,
                flawClassType: flaw_type_D[flawTypeIndex].id,
                dShape: flaw_type_D[flawTypeIndex].shape,
                cameraId: flaw_type_D[flawTypeIndex].camera_id,
                station: flaw_type_D[flawTypeIndex].station,
                show: flaw_type_D[flawTypeIndex].show,
                symbol: flaw_type_D[flawTypeIndex].symbol,
                color: flaw_type_D[flawTypeIndex].color,
                dCD: Math.random() * panelInfo.length_cd,
                dMD: Math.random() * panelInfo.length_md,
                dWidth,
                dLength,
                dArea: dWidth * dLength,
                dDiameter: Math.random() * 2,
                createTime: this.time,
                content: "http://192.168.1.200:5555/piantie.png",
            })
        }
        return flawArry;
    }
    // panel
    async createPanel(panelInfo, job) {
        ++this.cuurntDoff;
        if (this.cuurntDoff == this.doffSum) {
            this.createJob();
            this.cuurntDoff = 0;
        }
        const doff = {
            uid: panelInfo.uid,
            jobId: this.job.uid,
            flawCount: panelInfo.flawCount,
            startTime: panelInfo.startTime + 50000,
            finish_time: panelInfo.finish_time,
            detectParam: detectParam_2,
            exConfig1: panelInfo.exConfig1
        }
        const panel_info = {
            uid: panelInfo.uid,
            doff_id: panelInfo.uid,
            create_time: panelInfo.startTime,
            panel_id: panelInfo.panelId,
            lot_id: job.lot_number
        }
        await this.uploadInfo(Doff_1, doff, '偏贴后-Doff');
        await this.uploadInfo(Panel_info_1, panel_info, '偏贴后-panel_info');
    }
    // job
    async createJob() {
        this.doffSum = Math.floor(Math.random() * 20) + 10;
        let JobInfo = {
            uid: ++this.jobUid,
            jobName: `偏贴后工单${this.jobUid}`,
            orderNumber: `偏贴后批次${Math.floor(this.jobUid / 5) + 1}`,
            doffSum: this.doffSum,
            createTime: this.time,
            finishTime: this.time + 888,
            exConfig1: '{"job": {"modelSize": { "value": "8192;32000" } }}',
            lot_number: `LOTID0000${Math.floor(this.jobUid / 2) + 1}`
        }
        this.job = JobInfo;
        await this.uploadInfo(Job_1, JobInfo, '偏贴后-Job');
    }
    // 上传
    async uploadInfo(operater, jobInfo, tableName) {
        try {
            let res = await operater.upload(jobInfo);
            if (res.length || res.dataValues) {
                console.log(`${tableName} sucess`);
            }
        } catch (error) {
            console.log(error);
        }
    }
    static getInstance() {
        return new F();
    }
}

module.exports = F.getInstance();